// -----------------------------------------------------------------------------
// Wrapper for MFCC feature extractor
// -----------------------------------------------------------------------------
//
//  Copyright (C) 2016 D S Pavan Kumar
//  dspavankumar [at] gmail [dot] com
//
//  This program is free software: you can redistribute it and/or modify
//  it under the terms of the GNU General Public License as published by
//  the Free Software Foundation, either version 3 of the License, or
//  (at your option) any later version.
//
//  This program is distributed in the hope that it will be useful,
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//  GNU General Public License for more details.
//
//  You should have received a copy of the GNU General Public License
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.

#include <algorithm>
#include <iostream>
#include <fstream>
#include "../../common/mfccs/mfcc.cpp"


// Process each file
int processFile (MFCC &mfccComputer, const char* wavPath, const char* mfcPath) {
    // Initialise input and output streams    
    std::ifstream wavFp;
    std::ofstream mfcFp;
    
    // Check if input is readable
    wavFp.open(wavPath);
    if (!wavFp.is_open()) {
        std::cerr << "Unable to open input file: " << wavPath << std::endl;
        return 1;
    }
    
    // Check if output is writable
    mfcFp.open(mfcPath);
    if (!mfcFp.is_open()) {
        std::cerr << "Unable to open output file: " << mfcPath << std::endl;
        wavFp.close();
        return 1;
    }

    RK_U8 avsr_audio_input_data[48][64];
   
    // Extract and write features
    if (mfccComputer.process (wavFp, mfcFp, &avsr_audio_input_data[0][0]))
        std::cerr << "Error processing " << wavPath << std::endl;

    wavFp.close();
    mfcFp.close();
    return 0;
}

// Main
int main(int argc, char* argv[]) {

    char *wavPath = "/tmp/test.pcm";
    char *mfcPath ="/tmp/test.mfc";


    // Assign variables
    int numCepstra = 63;
    int numFilters =64;
    int samplingRate =16000;
    int winLength = 64;
    int frameShift = 34;
    int lowFreq = 50;
    int highFreq = samplingRate/2;

    // Initialise MFCC class instance
    MFCC mfccComputer (samplingRate, numCepstra, winLength, frameShift, numFilters, lowFreq, highFreq);

    // Process wav files
    if (wavPath && mfcPath)
        if (processFile (mfccComputer, wavPath, mfcPath))
            return 1;

    return 0;
}



